package com.nana.framework.net
{
	import com.adobe.serialization.json.JSON;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.utils.Timer;
	
	
	public class Statistics extends EventDispatcher
	{
		private var uid:int;
		private var sampling:Boolean = false;
		private var samplingProportion:Number = 1;
		private var clockInterval:uint = 0;
		private var timer:Timer;
		protected var clocking:Boolean = false;
		private var _httpUrl:String = "";
		private var urlRequest:URLRequest;
		private var urlLoader:URLLoader;
		private var urlRequestList:Array;
		private var lastRequestCompleted:Boolean = true;
		
		public function Statistics()
		{
			this.urlRequest = new URLRequest();
			this.urlLoader = new URLLoader();
			this.urlRequestList = [];
		}
		
		public function setProperty(_arg1:String, sampling:Boolean=false, clocking:Boolean=false, samplingProportion:Number=1, _arg5:uint=0):void{
			this.uid = this.cal(_arg1);
			this.sampling = sampling;
			this.samplingProportion = samplingProportion;
			this.clocking = clocking;
			this.clockInterval = _arg5;
			if (clocking){
				this.clockStart();
				this.timer = new Timer(_arg5);
				this.timer.addEventListener(TimerEvent.TIMER, this.clockIntervalHandler);
				this.timer.start();
			}
		}
		
		private function cal(_arg1:String):uint{
			var _local2:int = int(_arg1.substr((_arg1.length - 5), 5));
			return (_local2);
		}

		protected function clockStart():void{
		}
		
		protected function clockIntervalHandler(event:TimerEvent):void{
		}
		
		protected function sendStat(_arg1:String, _arg2:Object=null):void{
			if (this.sampling && (this.samplingProportion < 1)){
				if (this.uid % (1 / this.samplingProportion) != 0){
					return;
				}
			} else {
				this.sendStatHandler(_arg1, _arg2);
			}
		}
		
		private function sendStatHandler(_arg1:String, _arg2:Object=null):void{
			var _postData:* = null;
			var isHandler:* = false;
			var ioerrorHandler:* = null;
			var httpStatusHandler:* = null;
			var loadCompHandler:* = null;
			var _uv:* = null;
			var _name:* = null;
			var StatType:* = _arg1;
			var data = _arg2;
			if (!this.lastRequestCompleted){
				this.urlRequestList.push({
					StatType:StatType,
					data:data
				});
				return;
			}
			var dataJson:* = JSON.encode(data);
			var dataObj:* = {data:dataJson};
			if (dataObj is String){
				_postData = dataObj;
			} else {
				if ((dataObj is URLVariables)){
					_postData = dataObj;
				} else {
					_uv = new URLVariables();
					for (_name in dataObj) {
						_uv[_name] = dataObj[_name];
					}
					_postData = _uv;
				}
			}
			this.urlRequest.url = (this._httpUrl + StatType);
			this.urlRequest.method = URLRequestMethod.POST;
			this.urlRequest.data = _postData;
			isHandler = false;
			ioerrorHandler = function (event:IOErrorEvent):void{
				if (!isHandler){
					isHandler = true;
				}
			}
			httpStatusHandler = function (event:HTTPStatusEvent):void{
				if (!isHandler){
					isHandler = true;
				}
			}
			loadCompHandler = function (event:Event):void{
				if (!isHandler){
					isHandler = true;
				}
				lastRequestCompleted = true;
				clearAll();
				if (!(urlRequestList == null) && (urlRequestList.length > 0)){
					sendStatHandler(urlRequestList[0]["StatType"], urlRequestList[0]["data"]);
					urlRequestList.shift();
				}
			}
			var clearAll:* = function ():void{
				urlLoader.close();
				urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioerrorHandler);
				urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
				urlLoader.removeEventListener(Event.COMPLETE, loadCompHandler);
			}
			this.urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioerrorHandler);
			this.urlLoader.addEventListener(Event.COMPLETE, loadCompHandler);
			this.urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			this.urlLoader.dataFormat = "text";
			this.lastRequestCompleted = false;
			this.urlLoader.load(this.urlRequest);
		}

		public function get httpUrl():String
		{
			return _httpUrl;
		}

		public function set httpUrl(value:String):void
		{
			_httpUrl = value;
		}

	}
}